//
//  BXPAuthorizedLoginView.swift
//  XerxesPP
//
//  Created by 向攀 on 2017/7/12.
//  Copyright © 2017年 Boos－XP. All rights reserved.
//

import UIKit
import MBProgressHUD

class PPYAuthorizedLoginView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - setupUI
    func setupUI() -> Void{
        setupTopView()
        
        setupLoginWaysButtons()
    }
    
    func setupTopView() -> Void {
        let topTitleLabel = UILabel()
        topTitleLabel.font = UIFont.systemFont(ofSize: 15)
        topTitleLabel.textColor = UIColor.white
        topTitleLabel.text = "快速登录"
        topTitleLabel.textAlignment = NSTextAlignment.center
        addSubview(topTitleLabel)
        topTitleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.centerX.equalToSuperview()
            make.width.greaterThanOrEqualTo(10)
            make.width.lessThanOrEqualTo(66)
        }
        
        let leftLineView = UIImageView()
        leftLineView.image = UIImage(named: "login_register_left_line")
        addSubview(leftLineView)
        leftLineView.snp.makeConstraints { (make) in
            make.centerY.equalTo(topTitleLabel)
            make.left.equalToSuperview().offset(12)
            make.right.equalTo(topTitleLabel.snp.left).offset(-8)
            make.height.equalTo(1)
        }
        
        let rightLineView = UIImageView()
        rightLineView.image = UIImage(named: "login_register_right_line")
        addSubview(rightLineView)
        rightLineView.snp.makeConstraints { (make) in
            make.centerY.equalTo(topTitleLabel)
            make.right.equalToSuperview().offset(-12)
            make.left.equalTo(topTitleLabel.snp.right).offset(5)
            make.height.equalTo(1)
        }
    }
    
    func setupLoginWaysButtons() -> Void {
        let count = 3
        let buttonWH:CGFloat = 60
        let margin = (UIScreen.main.bounds.size.width - CGFloat(count) * buttonWH) / CGFloat(count + 1)
        
        let imageName = ["login_QQ_icon", "login_sina_icon", "login_QQ_icon"]
        let hightImageNmae = ["login_QQ_icon_click", "login_sina_icon_click", "login_QQ_icon_click"]
        let actions = [#selector(qqLoginButtonClicked(button:)), #selector(weiChatLoginButtonClicked(button:)), #selector(sinaLoginButtonClicked(button:))]
        
        let buttonY: CGFloat = 35
        
        var buttonX:CGFloat = 0
        
        for i in 0..<count {
            buttonX += margin
            buttonWithFrame(CGRect(x: buttonX, y: buttonY, width: buttonWH, height: buttonWH), imageName: imageName[i], highLightImageName: hightImageNmae[i], action: actions[i])
            buttonX += buttonWH
        }
        
    }
    
    func buttonWithFrame(_ buttonFrame: CGRect, imageName: String, highLightImageName: String, action: Selector) -> Void {
        let button = UIButton(frame: buttonFrame)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.setImage(UIImage(named: highLightImageName), for: .highlighted)
        
        button.addTarget(self, action: action, for: .touchUpInside)
        
        addSubview(button)
    }
    
    // MARK: - events response
    @objc private func qqLoginButtonClicked(button: UIButton) -> Void {
        MBProgressHUD.showAutoHideMessage(message: "该功能暂未开放,敬请期待", showBackground: false)
    }
    
    @objc private func weiChatLoginButtonClicked(button: UIButton) -> Void {
        MBProgressHUD.showAutoHideMessage(message: "该功能暂未开放,敬请期待", showBackground: false)
    }
    
    @objc private func sinaLoginButtonClicked(button: UIButton) -> Void {
        MBProgressHUD.showAutoHideMessage(message: "该功能暂未开放,敬请期待", showBackground: false)
    }

}
